package com.btashov.stringarray;

/**
 * @author Bahtiyar Tashov <btashov@gmail.com>
 */
public class ReverseStringUtils {

    private static final String STR_PARAM_NULL_OR_EMPTY = "Check string parameter, it's null or empty.";

    private static void checkNullAndEmptiness(String str) {
        if (str == null || str.isEmpty()) {
            throw new IllegalArgumentException(STR_PARAM_NULL_OR_EMPTY);
        }
    }

    public static String reverseString(String sentence) {
        new ReverseStringUtils().checkNullAndEmptiness(sentence);

        StringBuilder stringBuilder = new StringBuilder();
        for (char ch : sentence.toCharArray()) {
            stringBuilder.append(ch);
        }
        return stringBuilder.reverse().toString();
    }


    public static String reverse(String str) {
        checkNullAndEmptiness(str);

        StringBuilder strBuilder = new StringBuilder();
        char[] charArray = str.toCharArray();
        for (int index = charArray.length - 1; index >= 0; index--) {
            strBuilder.append(charArray[index]);
        }
        return strBuilder.toString();
    }

    public static String reverseStr(String str) {
        checkNullAndEmptiness(str);

        StringBuilder strBuilder = new StringBuilder(str.length());
        char[] charArray = str.toCharArray();
        for (int i = charArray.length; i > 0; i--) {
            strBuilder.append(charArray[i - 1]);
        }
        return strBuilder.toString();
    }


    public static String reverseFromSite(String sentence) {
        if (sentence == null || sentence.isEmpty()) {
            throw new IllegalArgumentException(STR_PARAM_NULL_OR_EMPTY);
        }

        return new StringBuilder(sentence).reverse().toString();
    }

    public static String reverseUsingCharArrayCopy(String input) {

        char[] reversed = input.toCharArray();
        for (int i = 0, j = reversed.length - 1; i < j; i++, j--) {
            reversed[i] = input.charAt(j);
            reversed[j] = input.charAt(i);
        }
        return new String(reversed);
    }

    public static String reverseWithoutCopy(String input) {

        char[] reversed = new char[input.length()];
        for (int i = 0, j = reversed.length - 1; i <= j; i++, j--) {
            reversed[i] = input.charAt(j);
            reversed[j] = input.charAt(i);
        }
        return new String(reversed);
    }


    public static String reverseWithSwapping(String input) {

        char[] chars = input.toCharArray();
        int right = chars.length - 1;
        for (int left = 0; left < right; left++) {
            char swap = chars[left];
            chars[left] = chars[right];
            chars[right--] = swap;
        }
        return new String(chars);
    }

    public static String reverseWithBuilder(String str) {

        StringBuilder reverse = new StringBuilder(str.length());
        for (int i = str.length() - 1; i >= 0; i--) {
            reverse.append(str.charAt(i));
        }
        return reverse.toString();
    }
}
